package com.aqielife.demo.config;

import com.aqielife.demo.handler.EchoHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author aqie
 * @date 2022/02/04 23:19
 * @desc
 */
@Configuration
public class WebSocketConfiguration {

  /**
   * SimpleUrlHandlerMapping 指定了 WebSocket 的路由配置；
   * 使用 map 指定 WebSocket 协议的路由，路由为 ws://localhost:8080/echo。
   * @param echoHandler
   * @return
   */
  @Autowired
  @Bean
  public HandlerMapping webSocketMapping(final EchoHandler echoHandler) {
    final Map<String, WebSocketHandler> map = new HashMap<>();
    map.put("/echo", echoHandler);

    final SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
    mapping.setUrlMap(map);
    return mapping;
  }

  /**
   * WebSocketHandlerAdapter 负责将 EchoHandler 处理类适配到 WebFlux 容器中；
   * @return
   */
  @Bean
  public WebSocketHandlerAdapter handlerAdapter() {
    return new WebSocketHandlerAdapter();
  }
}
